home *** CD-ROM | disk | FTP | other *** search
- Path: news-relay.eworld.com!zdc!zippo!drn
- From: Clarence Chiang
- Newsgroups: comp.lang.c++
- Subject: Re: Doubly Linked List help - please!
- Date: 1 Apr 1996 12:16:00 -0800
- Organization: Zippo
- Sender: http@doc.zippo.com
- Message-ID: <4jpdi0$360@doc.zippo.com>
- NNTP-Posting-Host: doorstop.spiderisland.com
-
- In article <internews46B70D427A@argonet.co.uk>, Charlotte says...
- >
- >I have implemented a template doubly linked list, the backbones of which
- >is as follows:
- >
- >//dllist.h
- >
- >/*snip*/
- >
- >template <class T> class node
- >{
- > public:
- > T data;
- > node<T> *next;
- > node<T> *prev;
- > node();
- > node(T info);
- > node<T> *getnext() const {return next;}
- > node<T> *getprev() const {return prev;}
- > T getinfo() const {return data;}
- > void change(T info) {data=info;}
- >/*other functions*/
- >};
- >
- >template <class T> class dllist : public node<T>
- >{
- > node<T> *start, *end;
- > void copy(const dllist<T>);
- >
- > public:
- > dllist() {start=end=NULL;}
- > dllist(const dllist<T>&);
- > int isEmpty() const;
- >/*store, remove, find, ==, etc. snipped*/
- >};
- >
- >
- >Then I have a class, called fuzzyset, which uses this dll as a container
- >for fuzzyel instances:
- >
- >//fuzzyset.h
- >/*snip*/
- >
- >class fuzzyset {
- > private:
- > dllist<fuzzyel> members;
- >
- > public:
- > fuzzyset() {}
- > fuzzyset(const fuzzyset&);
- > fuzzyset(const fuzzyel&);
- >/*other functions and operators*/
- >};
- >
- >
- >What I would like to do is, within dllist, to implement a function forEach
- >that take a pointer to a function as one argument, and somehow the rest of
- >the arguments required by that function, and carries that function out
- >over each element of the list.
- >
- >One example of what I need to do is as follows. In my main program (where
- >fuzzyset is included), I need to be able to call a function that prints
- >every element of the 'members' list in any called instance of a fuzzyset.
- >
- >I need the forEach function to be quite generic, because I will have other
- >uses for it later.
- >
- >I hope someone can help. Many thanks in advance.
- >
- >
- >
- >
- >
- >... So, how did I do?
- >--
- >-----------------------------------------------------------------------------
- >| Charlotte Tomlinson | Mediocrity know nothing higher than itself, |
- >| eeyore@argonet.co.uk | but talent instantly recognises genius. |
- >|---------------------------------------------------------------------------|
- >--------- Now WWWebbed up at http://www.argonet.co.uk/users/eeyore/ ---------
- >
-
- Why don't you implement an iterator class that's friend class of dllList ? In
- that way you don't have to expose anything (data member or tranversal member
- functions) and still be able to apply functions to each element.
-
- If you look into the STL, there will be a lot of examples of how to write and use
- iterators.
-
- Clarence Chiang
- Spider Island Software
-
-